package com.ruoyi.web.controller.column;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.SysConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.column.domain.SysColumn;
import com.ruoyi.column.service.ISysColumnService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 专栏Controller
 * 
 * @author yjf
 */
@RestController
@RequestMapping("/column")
public class SysColumnController extends BaseController
{
    @Autowired
    private ISysColumnService sysColumnService;

    /**
     * 查询专栏列表
     */
    @PreAuthorize("@ss.hasPermi('column:column:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysColumn sysColumn)
    {
        startPage();
        List<SysColumn> list = sysColumnService.selectSysColumnList(sysColumn);
        return getDataTable(list);
    }

    /**
     * 查询全部专栏列表
     */
    @PreAuthorize("@ss.hasPermi('column:column:list')")
    @GetMapping("/wholeColumn")
    public AjaxResult wholeColumn(SysColumn sysColumn)
    {
        return AjaxResult.success(sysColumnService.selectSysColumnList(sysColumn));
    }
    /**
     * 导出专栏列表
     */
    @PreAuthorize("@ss.hasPermi('column:column:export')")
    @Log(title = "专栏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysColumn sysColumn)
    {
        List<SysColumn> list = sysColumnService.selectSysColumnList(sysColumn);
        ExcelUtil<SysColumn> util = new ExcelUtil<SysColumn>(SysColumn.class);
        util.exportExcel(response, list, "专栏数据");
    }

    /**
     * 获取专栏详细信息
     */
    @PreAuthorize("@ss.hasPermi('column:column:query')")
    @GetMapping(value = "/{columnId}")
    public AjaxResult getInfo(@PathVariable("columnId") Long columnId)
    {
        return AjaxResult.success(sysColumnService.selectSysColumnByColumnId(columnId));
    }

    /**
     * 新增专栏
     */
    @PreAuthorize("@ss.hasPermi('column:column:add')")
    @Log(title = "专栏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysColumn sysColumn)
    {
        //专栏名称唯一
        if (SysConstants.CONSTANT_STR1.equals(sysColumnService.checkColumnNameUnique(sysColumn)))
        {
            return AjaxResult.error("新增专栏：'" + sysColumn.getColumnName() + "'失败，名称已存在");
        }
        return toAjax(sysColumnService.insertSysColumn(sysColumn));
    }

    /**
     * 修改专栏
     */
    @PreAuthorize("@ss.hasPermi('column:column:edit')")
    @Log(title = "专栏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysColumn sysColumn)
    {
        //专栏名称唯一
        if (SysConstants.CONSTANT_STR1.equals(sysColumnService.checkColumnNameUnique(sysColumn)))
        {
            return AjaxResult.error("新增专栏：'" + sysColumn.getColumnName() + "'失败，名称已存在");
        }
        return toAjax(sysColumnService.updateSysColumn(sysColumn));
    }

    /**
     * 删除专栏
     */
    @PreAuthorize("@ss.hasPermi('column:column:remove')")
    @Log(title = "专栏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{columnIds}")
    public AjaxResult remove(@PathVariable Long[] columnIds)
    {
        return toAjax(sysColumnService.deleteSysColumnByColumnIds(columnIds));
    }
}
